package com.wn.service;

import com.wn.mapper.*;
import com.wn.ov.PatientOv;
import com.wn.pojo.Appointment;
import com.wn.pojo.HospitalizedApplication;
import com.wn.pojo.PatientDetail;
import com.wn.pojo.Room;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 出院处理业务
 */
@Service
public class HospitalizedApplicationService {
    @Autowired
    private HospitalizedApplicationMapper ham;

    @Autowired
    private MedicalAdviceMapper medicalAdviceMapper;

    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private PatientDetailMapper patientDetailMapper;

    /**
     * 出院申请
     *
     * @param
     * @return
     */
    public ServerResponse addHa(PatientOv patientOv) {
        HospitalizedApplication ha = ham.selectByPatientId(patientOv.getId());
        ha.setStatus(2);
        int insert = ham.updateByPrimaryKeySelective(ha);
        if (insert != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "申请成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "申请失败");
        }
    }

    public ServerResponse addHa(HospitalizedApplication ha) {
        ha.setStatus(2);
        int insert = ham.updateByPrimaryKeySelective(ha);
        if (insert != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "申请成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "申请失败");
        }
    }

    /**
     * 获取申请
     *
     * @param
     * @return
     */
    public ServerResponse getHa(PatientOv patientOv) {
        HospitalizedApplication ha = ham.selectByPatientId(patientOv.getId());
        if (!StringUtils.isEmpty(ha)) {
            return new ServerResponse(ResponseCode.SUCCESS, ha, "成功");
        } else {
            return new ServerResponse(ResponseCode.SUCCESS, "失败");
        }
    }

    //护士停止其医嘱
    public ServerResponse updateStatus(Integer id) {
        int update = medicalAdviceMapper.updateStatus(id);
        if (update != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "修改失败");
        }
    }

    //床位状态的修改
    public ServerResponse updateIsUsed(HospitalizedApplication ha, Integer id) {
        Room room = new Room();
        room.setId(ha.getRoomId());
        room.setIsUsed(1);
        room.setUpdateUserId(id);
        //修改床位的状态
        int update = roomMapper.updateByPrimaryKeySelective(room);
        if (update != 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "修改失败");
        }
    }

    /**
     * 入院申请
     *
     * @param hospitalizedApplication
     * @return
     */
    public ServerResponse addHospitalizedApplication(HospitalizedApplication hospitalizedApplication) {
        if (StringUtils.isEmpty(ham.selectByPatientId(hospitalizedApplication.getPatientId()))) {
            hospitalizedApplication.setStatus(0);
            ham.insertSelective(hospitalizedApplication);
//            //生成病人详情
//            PatientDetail patientDetail = new PatientDetail();
//            patientDetail.setPatientId(hospitalizedApplication.getPatientId());
//            patientDetail.setDoctorId(hospitalizedApplication.getDoctorId());
//            Appointment appointment=appointmentMapper.selectByPId(hospitalizedApplication.getPatientId());
//            patientDetail.setDepartmentId(appointment.getDepartmentId());
//            patientDetailMapper.insertSelective(patientDetail);

            return new ServerResponse(ResponseCode.SUCCESS, "添加成功");
        }
        return new ServerResponse(ResponseCode.ERROR, "不能重复添加");
    }


    public ServerResponse delHospitalizedApplicationOut(PatientOv patientOv) {
        HospitalizedApplication hospitalizedApplication = ham.selectByPatientId(patientOv.getId());
        if (hospitalizedApplication.getStatus() == 0) {
            ham.deleteByPrimaryKey(hospitalizedApplication.getId());
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        } else if (hospitalizedApplication.getStatus() == 2) {
            hospitalizedApplication.setStatus(1);
            ham.updateByPrimaryKeySelective(hospitalizedApplication);
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        }
        return new ServerResponse(ResponseCode.ERROR, "请先添加出/入院申请");
    }
}
